package mipo.top.base.service.impl;

import lombok.extern.slf4j.Slf4j;
import mipo.top.base.Dto.EmailDto;
import mipo.top.base.Dto.EmailRegisterDto;
import mipo.top.base.Dto.PhoneRegisterDto;
import mipo.top.base.constant.LoginConstant;
import mipo.top.base.constant.RegisterConstant;
import mipo.top.base.exception.BusinessException;
import mipo.top.base.service.IVerifyCodeUtilsService;
import mipo.top.base.util.Base64Util;
import mipo.top.base.util.SimpleMailMsg;
import mipo.top.base.util.StrUtils;
import mipo.top.base.util.VerifyCodeUtils;
import mipo.top.users.domain.User;
import mipo.top.users.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VerifyCodeUtilsServiceimpl implements IVerifyCodeUtilsService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;


    @Override
    public String ImgCode(String key) {
        String randomCode = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(key,randomCode,300, TimeUnit.SECONDS);
        String verifyCode = VerifyCodeUtils.VerifyCode(100, 35, randomCode);
        return verifyCode;
    }

    @Override
    public void SmsCode(PhoneRegisterDto phoneRegisterDto) {

        if ("register".equals(phoneRegisterDto.getType())){
            String phone = getString(phoneRegisterDto);
            if (phoneRegisterDto.getImageCodeValue()==null){
                throw new BusinessException("图形验证码不能为空");
            }
            if (phoneRegisterDto.getImageCodeKey()==null){
                throw new BusinessException("UUID不能为空");
            }
            String smsCodeKey = RegisterConstant.PHONEREGISTERKEY+phone;
            verfiy(smsCodeKey);
        }else if ("binder".equals(phoneRegisterDto.getType())){
            String smsCodeKey = RegisterConstant.BINDERREGISTERKEY+phoneRegisterDto.getPhone();
            verfiy(smsCodeKey);
        }
    }

    private void verfiy(String smsCodeKey) {
        String s = (String) redisTemplate.opsForValue().get(smsCodeKey);
        String smsCode="";
        if (s==null){
            smsCode=StrUtils.getRandomString(4);
        }else {
            String[] strings = s.split(":");
            Long oldtime = Long.valueOf(strings[1]);
            Long nowtime = System.currentTimeMillis();
            if(nowtime-oldtime<=60*1000){
                throw new BusinessException("两次验证码重复发送时间不得小于60秒");
            }
            smsCode=strings[0];
        }
        String smsCodeValue = smsCode+":"+System.currentTimeMillis();
        redisTemplate.opsForValue().set(smsCodeKey,smsCodeValue,5,TimeUnit.MINUTES);
        System.out.println(smsCode);
        log.info("你的验证码是"+smsCode+"，请在5分钟内使用");
    }

    private String getString(PhoneRegisterDto phoneRegisterDto) {
        String phone = phoneRegisterDto.getPhone();
        User findphone = userMapper.findphone(phone);
        if (findphone!=null){
            throw new BusinessException("该用户已经注册过了");
        }
        String imageCode = (String) redisTemplate.opsForValue().get(phoneRegisterDto.getImageCodeKey());
        if (!imageCode.equalsIgnoreCase(phoneRegisterDto.getImageCodeValue())){
            throw new BusinessException("验证码输入错误");
        }
        return phone;
    }

    @Override
    public void emailCode(EmailRegisterDto dto) throws Exception {
        String email = dto.getEmail();
        User user = userMapper.findemail(email);
        if (user!=null){
            throw  new BusinessException("邮箱已经被激活过了");
        }
        SimpleMailMsg.emailRegister(dto);
    }

    @Override
    public void phoneCode(String phone) {
        if (phone==null){
            throw new BusinessException("手机号码不能为空");
        }
        String phoneCode = StrUtils.getComplexRandomString(4);
        String phoneCodeKey = LoginConstant.PHONELOGINKEY+phone;
        redisTemplate.opsForValue().set(phoneCodeKey,phoneCode,5,TimeUnit.MINUTES);

    }
}
